/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package strategies;

import dao.DepartmentDAO;
import dao.EmployeeDAO;
import exceptions.DatabaseException;
import java.util.List;
import wrappers.DepartmentWrap;
import wrappers.EmployeeWrap;

/**
 *
 * @author KAV
 */
public class EmployeesEditPageStrategy extends EditPageStrategy {

    @Override
    protected void commitRow(Object e) throws DatabaseException {
        EmployeeWrap ew;
        if (e == null) {
            ew = new EmployeeWrap();
        } else {
            ew = (EmployeeWrap) e;
        }

        op.putBlockLabel("edit_name", "Name");
        op.putTextField("edit_name", ew.getName());
        op.putBlockLabel("edit_surname", "Surname");
        op.putTextField("edit_surname", ew.getSurname());
        op.putBlockLabel("edit_job", "Job");
        op.putTextField("edit_job", ew.getJob());
        op.putBlockLabel("edit_manager", "Manager");
        op.echo("<select class=\"inline\" id=\"edit_manager\" name=\"edit_manager\" size=\"1\">");
        op.putSelectOption("0", "No manager", ew.getManager() == 0);
        dao = new EmployeeDAO();
        List<Object> list = dao.findAll();
        for (Object o : list) {
            EmployeeWrap obj = (EmployeeWrap) o;
            if (editId.toString().compareTo(obj.getViewId().toString()) != 0) {
                op.putSelectOption(obj.getViewId().getValue(), obj.getViewName().getValue() + " " + obj.getViewSurname().getValue(), ew.getManager() == Integer.parseInt(obj.getViewId().getValue()));
            }
        }
        op.echo("</select><div class=\"clear\"></div>");
        op.putBlockLabel("", "Hire date:");
        op.putInlineLabel("edit_day", "Day");
        op.echo("<select class=\"inline\" name=\"edit_day\" id=\"edit_day\" size=\"1\">");
        for (int i = 1; i < 32; i++) {
            op.putSelectOption(leadingZero(i), Integer.toString(i), i == day);
        }
        op.echo("</select>");
        op.putInlineLabel("edit_month", "Month");
        op.echo("<select class=\"inline\" name=\"edit_month\" id=\"edit_month\" size=\"1\">");
        for (int i = 1; i < 13; i++) {
            op.putSelectOption(leadingZero(i), Integer.toString(i), i == month);
        }
        op.echo("</select>");
        op.putInlineLabel("edit_year", "Year");
        op.echo("<select class=\"inline\" name=\"edit_year\" id=\"edit_year\" size=\"1\">");
        for (int i = 2011; i > 1970; i--) {
            op.putSelectOption(leadingZero(i), Integer.toString(i), i == year);
        }
        op.echo("</select><div class=\"clear\"></div>");
        op.putBlockLabel("edit_salary", "Salary");
        op.putTextField("edit_salary", Integer.toString(ew.getSalary()));
        op.putBlockLabel("edit_department", "Department");
        op.echo("<select class=\"inline\" id=\"edit_department\" name=\"edit_department\" size=\"1\">");
        dao = new DepartmentDAO();
        list = dao.findAll();
        for (Object o : list) {
            DepartmentWrap obj = (DepartmentWrap) o;
            op.putSelectOption(obj.getViewId().getValue(), obj.getViewTitle().getValue(), ew.getDepartment() == Integer.parseInt(obj.getViewId().getValue()));
        }
        op.echo("</select><div class=\"clear\"></div>");
    }

    @Override
    protected Object writeInfo() {
        EmployeeWrap ew = (EmployeeWrap) e;
        String s = ew.getDate().substring(0, 4);
        year = Integer.parseInt(s);
        s = ew.getDate().substring(5, 7);
        month = Integer.parseInt(s);
        s = ew.getDate().substring(8, 10);
        day = Integer.parseInt(s);
        return ew;
    }
}
